// pages/certificate/certificate.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
	certificate:[],
	leftList: [],
	rightList: [],
	leftHight: 0,
	rightHight: 0,
    start:0,
    page: 1,
    size: 8,
    // allNum: 0,
    isEnd: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	this.getData(this.data.leftHight,
		this.data.rightHight,
		this.data.leftList,
		this.data.rightList,
		this.data.start);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

//获取更多
	this.data.start = this.data.page * this.data.size;
	this.getData(this.data.leftHight,
				this.data.rightHight,
				this.data.leftList,
				this.data.rightList,
				this.data.start);
	
	this.data.page += 1;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData:function(lastLH,lastRH,lastLeft,lastRight,start){
	  var that = this;
	  var url_ = getApp().globalData.url_;
	  if (this.data.isEnd){
	    return
	  }
	  wx.request({
	    url:  url_ + '/api/certificate?start='+ start +'&count='+ that.data.size,
	    method: "GET",
	    dataType: "json",
		header: {
		'content-type': 'application/json' // 默认值
		},
	    success: function(res){
	        var allData = res.data.data;
			// console.log(allData)
			if(allData.length <  that.data.size || !allData){
				that.setData({
				isEnd: true
				})
			}
	        //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
	        var leftH = lastLH;
	        var rightH = lastRH;
	        var leftData = [];
	        var rightData = [];
			var CoverWidth = 0;
			var CoverHeight = 0;
	        for (let i = 0; i < allData.length; i++) {
				var img = url_ + allData[i].image;
				allData[i].image = img;
				CoverWidth = parseInt(allData[i].imagewidth)
				CoverHeight = parseInt(allData[i].imageheight);
				var currentItemHeight = parseInt((CoverHeight * 375) /CoverWidth);
				if (leftH == rightH || leftH < rightH) {//判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
					leftData.push(allData[i]);
					leftH += currentItemHeight;
				} else {
					rightData.push(allData[i]);
					rightH += currentItemHeight;
				}
	        }
			//新旧数据合并
			var lastLeftData = lastLeft;
			var lastRightData = lastRight;
			lastLeftData.push(...leftData);
			lastRightData.push(...rightData);
	     //更新左右两栏的数据以及累计高度
	   
	  	that.setData({
	  		certificate: res.data.data,
	  		leftHight: leftH,
	  		rightHight: rightH,
	  		leftList: lastLeftData,
	  		rightList: lastRightData
	  	})
	    },
	    fail:function(e){
	      console.log("请求失败");
	    },
	  })
	  
  },
  //点击放大
	preview:function(e){
		var src = e.currentTarget.dataset.src;  
		var imgList = e.currentTarget.dataset.list;  
		var str=""; var newArr = [];   
		// str = src + "," + imgList;
		str = src;
		newArr = str.split(",");
		wx.previewImage({ 
			current: newArr,  // 当前显示图片的http链接
			urls: newArr     // 需要预览的图片http链接列表
		})
		 
	},

})